package com.midea.ai.air.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.midea.ai.air.R;

import java.util.List;

/**
 * Created by ouyangdi on 2016/4/11.
 * 底部弹出对话框View
 */
public class BottomDialog {
    private Context mContext;
    private Dialog mDialog;
    private TextView mTvTitle;
    private ListView mListItems;
    private Button mBtnCancel;
    private View mTitleBotLine;

    private Boolean mIsShowTitle = false;  //是否显示标题
    private List<ListItem> mItems;

    public BottomDialog(Context context) {
        mContext = context;
    }

    public BottomDialog builder() {
        //获取Dialog布局
        View view = View.inflate(mContext, R.layout.layout_bot_dialog, null);

        //获取自定义Dialog布局中的控件
        mTvTitle = (TextView)view.findViewById(R.id.tv_bot_dialog_title);
        mListItems = (ListView)view.findViewById(R.id.lv_bot_dialog_items);
        mListItems.setBackgroundResource(R.drawable.bg_cancel_edit_room);

        mBtnCancel = (Button)view.findViewById(R.id.btn_bot_dialog_cancel);
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        mTitleBotLine = view.findViewById(R.id.bot_dialog_title_line);
        //定义Dialog布局和参数
        mDialog = new Dialog(mContext, R.style.bottom_view_style);
        mDialog.setContentView(view);

        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);

        return this;
    }

    public BottomDialog setTitle(String title) {
        mIsShowTitle = true;
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText(title);
        mListItems.setBackgroundResource(R.drawable.bg_other_room_edit);
        mTitleBotLine.setVisibility(View.VISIBLE);
        return this;
    }

    public BottomDialog setCanceledOnTouchOutside(boolean cancel) {
        mDialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public BottomDialog addListItems(List<ListItem> items) {
        mItems = items;
        ItemsAdapter adapter = new ItemsAdapter(mContext, 0, items);
        mListItems.setAdapter(adapter);
        return this;
    }

    public void show() {
        mDialog.show();
    }

    public void dismiss() {
        if(mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener itemClickListener) {
        if(mListItems != null) {
            mListItems.setOnItemClickListener(itemClickListener);
        }
    }

    private class ItemsAdapter extends ArrayAdapter<ListItem>{

        public ItemsAdapter(Context context, int resource, List<ListItem> objects) {
            super(context, resource, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Button btnItem;
            final ListItem item = mItems.get(position);
            if(convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_room_edit, null);
                btnItem = (Button)convertView.findViewById(R.id.btn_bot_dialog_item);
                convertView.setTag(btnItem);
            } else {
                btnItem = (Button)convertView.getTag();
            }

            btnItem.setText(item.getStrName());
            if(item.getTextColor() != 0) {
                btnItem.setTextColor(item.getTextColor());
            }

            if(!mIsShowTitle && getCount() == 1) {
                btnItem.setBackgroundResource(R.drawable.bg_cancel_edit_room);
            } else {
                if (!mIsShowTitle && position == 0) {
                    btnItem.setBackgroundResource(R.drawable.bg_bot_dialog_first);
                }

                if (position == mItems.size() - 1) {
                    btnItem.setBackgroundResource(R.drawable.bg_other_room_edit);
                } else {
                    btnItem.setBackgroundResource(R.drawable.bg_list_item_selector);
                }
            }
            return convertView;
        }
    }

    public static class ListItem {
        private String strName;
        private int textColor = 0;

        public ListItem(String strName) {
            this.strName = strName;
        }

        public String getStrName() {
            return strName;
        }

        public ListItem(String strName, int textColor) {
            this.strName = strName;
            this.textColor = textColor;
        }
        public int getTextColor() {
            return textColor;
        }
    }
}
